/**
 * @description 商品JS
 * @author 赵子靖
 * @since 2017-12-06
 */
var Shop = function() {
    var t = {
        path :'',
        editor:'',
        init : function() {
            t.initView();
            t.initEvent();
        },
        initView : function() {
            
        },
        initEvent : function() {
            
        },
        initTablePage:function(){
            window.location.href=t.path+"/busShop/forwardShop.do?data="+new Date();
        },
        //初始化商品类别树
        initShopTypeTree:function(){
            ComTree.initTree($.extend({
                divContainer : "#shop-type-tree",
                url : t.path+'/busShopType/getShopTypesTree.do',
                selectedFirstNode : true,
                p : {
                },
                onClick : function(e, treeId, treeNode) {
                    $("#shopType").val(treeNode.name);
                    $("#shopType").attr("ids",treeNode.id);
                }
            }));
        },
        //加载数据
        render:function(){
            $("#shop-table").bootstrapTable('destroy');
            $("#shop-table").bootstrapTable({
                queryParams : function(params) {
                    $.extend(params, {
                    })
                    return params;
                }
            });
            $("#shop-table").bootstrapTable('refresh');
        },
        //添加
        addShopDialog:function(){
            window.location.href=t.path+"/busShop/forwardAddShop.do";
            t.initShopTypeTree();
        },
        //显示树
        showShopTypeTree:function(){
            $("#shopTypeContent").slideDown("fast");
            $("body").bind("mousedown",function(event){
                if (!(event.target.id == "menuBtn" || event.target.id == "shopTypeContent" || $(event.target).parents("#shopTypeContent").length > 0)) {
                    $("#shopTypeContent").fadeOut("fast");
                    $("body").unbind("mousedown", null);
                }
            });
        },
        //保存
        addShop:function(){
            var sName = $("#add-shop-dialog #sName").val();
            var sMobileName = $("#add-shop-dialog #sMobileName").val();
            var sNo = $("#add-shop-dialog #sNo").val();
            var sShopTypeId = $("#shopType").attr("ids");//商品类别
            var sShopKeyWord = $("#add-shop-dialog #sShopKeyWord").val();
            //商品标签
            var shopLabels = '';
            $("#shopLabelDiv input:checkbox").each(function(){
                if($(this).prop('checked')){
                    shopLabels+=$(this).val()+',';
                }
            })
            var sShopInventoryNum = $("#add-shop-dialog #sShopInventoryNum").val();//库存
            var aShopSellNum = $("#add-shop-dialog #aShopSellNum").val();//销售量
            var sIsPutaway = $("input[name='sIsPutaway']:checked").val();//上下架
            var sSort = $("#add-shop-dialog #sSort").val();//排序
            //商品价格
            var spUnit = $("#add-shop-dialog #spUnit").val();//商品单位
            var spSpecification = $("#add-shop-dialog #spSpecification").val();//商品规格
            var spMarketPrice = $("#add-shop-dialog #spMarketPrice").val();//商品市场单价
            var spCostPrice = $("#add-shop-dialog #spCostPrice").val();//商品成本单价
            var spPrice = $("#add-shop-dialog #spPrice").val();//商品单价
            var spUnitNum = $("#add-shop-dialog #spUnitNum").val();//数量转换
            var spLevelDiamondPrice = $("#add-shop-dialog #spLevelDiamondPrice").val();//钻石会员价格
            var spLevelGoldPrice = $("#add-shop-dialog #spLevelGoldPrice").val();//金牌会员价格
            var spLevelSilverPrice = $("#add-shop-dialog #spLevelSilverPrice").val();//银牌会员价格
            var spLevelCopperPrice = $("#add-shop-dialog #spLevelCopperPrice").val();//铜牌会员
            //商品图片
            var sDesc = Shop.editor.html();//商品描述
            
            if(Util.isEmpty(sName)){
                $("#sNameSpan").html("请输入商品名称。");
                $("#add-shop-dialog #sName").focus();
                return;
            }
            $("#sNameSpan").html("");
            if(Util.isEmpty(sMobileName)){
                $("#sMobileNameSpan").html("请输入商品名称。");
                $("#add-shop-dialog #sMobileName").focus();
                return;
            }
            $("#sMobileNameSpan").html("");
            if(Util.isEmpty(sNo)){
                $("#sNoSpan").html("请输入商品编号。");
                $("#add-shop-dialog #sNo").focus();
                return;
            }
            $("#sNoSpan").html("");
            if(Util.isEmpty(sShopInventoryNum)){
                $("#sShopInventoryNumSpan").html("请输入商品库存数量。");
                $("#add-shop-dialog #sShopInventoryNum").focus();
                return;
            }
            $("#sShopInventoryNumSpan").html("");
            
            if(Util.isEmpty(spUnit)){
                $("#spUnitSpan").html("请输入商品单位。");
                $("#add-shop-dialog #spUnit").focus();
                return;
            }
            $("#spUnitSpan").html("");
            if(Util.isEmpty(spSpecification)){
                $("#spSpecificationSpan").html("请输入商品单位。");
                $("#add-shop-dialog #spSpecification").focus();
                return;
            }
            $("#spSpecificationSpan").html("");
            if(Util.isEmpty(spUnit)){
                $("#spUnitSpan").html("请输入商品规格。");
                $("#add-shop-dialog #spUnit").focus();
                return;
            }
            $("#spSpecificationSpan").html("");
            if(Util.isEmpty(spMarketPrice)){
                $("#spMarketPriceSpan").html("请输入商品。");
                $("#add-shop-dialog #spMarketPrice").focus();
                return;
            }
            $("#spMarketPriceSpan").html("");
            if(Util.isEmpty(spPrice)){
                $("#spPriceSpan").html("请输入商品市场价。");
                $("#add-shop-dialog #spPrice").focus();
                return;
            }
            $("#spPriceSpan").html("");
            if(Util.isEmpty(spUnitNum)){
                $("#spUnitNumSpan").html("请输入商品数量转换。");
                $("#add-shop-dialog #spUnitNum").focus();
                return;
            }
            $("#spUnitNumSpan").html("");
            if(Util.isEmpty(spLevelDiamondPrice)){
                $("#spLevelDiamondPriceSpan").html("请输入钻石会员价格。");
                $("#add-shop-dialog #spLevelDiamondPrice").focus();
                return;
            }
            $("#spLevelDiamondPriceSpan").html("");
            if(Util.isEmpty(spLevelGoldPrice)){
                $("#spLevelGoldPriceSpan").html("请输入金牌会员价格。");
                $("#add-shop-dialog #spLevelGoldPrice").focus();
                return;
            }
            $("#spLevelGoldPriceSpan").html("");
            if(Util.isEmpty(spLevelSilverPrice)){
                $("#spLevelSilverPriceSpan").html("请输入银牌会员价格。");
                $("#add-shop-dialog #spLevelSilverPrice").focus();
                return;
            }
            $("#spLevelSilverPriceSpan").html("");
            if(Util.isEmpty(spLevelCopperPrice)){
                $("#spLevelCopperPriceSpan").html("请输入铜牌会员价格。");
                $("#add-shop-dialog #spLevelCopperPricePrice").focus();
                return;
            }
            $("#spLevelCopperPriceSpan").html("");
            
            if(Util.isEmpty(sDesc)){
                $("#sDescSpan").html("请输入商品描述信息。");
                $("#add-shop-dialog #sDesc").focus();
                return;
            }
            $("#sDescSpan").html("");
            
            $.ajaxFileUpload({
                url:t.path+"/busShop/addShop.do",
                secureuri:false,
                fileElementId:["sImgSmall","sImgBig1","sImgBig2","sImgBig3","sImgBig4","sImgBig5"],
                dataType:"json",
                cache:false,
                data:{
                    sName:sName,
                    sMobileName:sMobileName,
                    sNo:sNo,
                    sShopTypeId:sShopTypeId,
                    sShopKeyWord:sShopKeyWord,
                    shopLabels:shopLabels,
                    sShopInventoryNum:sShopInventoryNum,
                    aShopSellNum:aShopSellNum,
                    sIsPutaway:sIsPutaway,
                    sSort:sSort,
                    spUnit:spUnit,
                    spSpecification:spSpecification,
                    spMarketPrice:spMarketPrice,
                    spCostPrice:spCostPrice,
                    spPrice:spPrice,
                    spUnitNum:spUnitNum,
                    spLevelDiamondPrice:spLevelDiamondPrice,
                    spLevelGoldPrice:spLevelGoldPrice,
                    spLevelSilverPrice:spLevelSilverPrice,
                    spLevelCopperPrice:spLevelCopperPrice,
                    sDesc:sDesc
                },
                success:function(data,status){//服务器响应成功处理
                    if(data.success){
                        $.ajax({
                            type:"POST",
                            url:t.path+"/busShop/saveShopDesc.do",
                            dataType:"json",
                            data:{
                                sId:data.item.sId,
                                sDesc:sDesc
                            },
                            async: false,
                            cache:false,
                            success:function(data,status){
                                if(data.success){
                                    alert("保存成功!");
                                    t.initTablePage();
                                }else{
                                    alert("保存失败。");
                                }
                            }
                        });
                    }else{
                        alert("保存失败!");
                    }
                },
                error:function(data,status,e){//服务器响应失败处理
                    alert(e);   
                }
            });
        },
        //编辑节点
        updateShopDialog:function(id){
            window.location.href=t.path+"/busShop/forwardUpdateShop.do?id="+id;
        },
        //反选
        selectShopTypeCheckbox:function(shopId){
            $.ajax({
                type:"POST",
                url:t.path+"/busShopLabelRel/findShopLabelRelList.do",
                dataType:"json",
                data:{
                    shopId:shopId
                },
                async: false,
                cache:false,
                success:function(data){
                    if(data && data.rows){
                        for(var i=0;i<data.rows.length;i++){
                            $("#"+data.rows[i].slrShopLabelId).prop("checked",true);
                        }
                    }
                }
            });
        },
        //修改
        updateShop:function(){
            var sId = $("#update-shop-dialog #sId").val();
            var sName = $("#update-shop-dialog #sName").val();
            var sMobileName = $("#update-shop-dialog #sMobileName").val();
            var sNo = $("#update-shop-dialog #sNo").val();
            var sShopTypeId = $("#shopType").attr("ids");//商品类别
            var sShopKeyWord = $("#update-shop-dialog #sShopKeyWord").val();
            //商品标签
            var shopLabels = '';
            $("#shopLabelDiv input:checkbox").each(function(){
                if($(this).prop('checked')){
                    shopLabels+=$(this).val()+',';
                }
            })
            var sShopInventoryNum = $("#update-shop-dialog #sShopInventoryNum").val();//库存
            var aShopSellNum = $("#update-shop-dialog #aShopSellNum").val();//销售量
            var sIsPutaway = $("input[name='sIsPutaway']:checked").val();//上下架
            var sSort = $("#update-shop-dialog #sSort").val();//排序
            //商品价格
            var spUnit = $("#update-shop-dialog #spUnit").val();//商品单位
            var spSpecification = $("#update-shop-dialog #spSpecification").val();//商品规格
            var spMarketPrice = $("#update-shop-dialog #spMarketPrice").val();//商品市场单价
            var spCostPrice = $("#update-shop-dialog #spCostPrice").val();//商品成本单价
            var spPrice = $("#update-shop-dialog #spPrice").val();//商品单价
            var spUnitNum = $("#update-shop-dialog #spUnitNum").val();//数量转换
            var spLevelDiamondPrice = $("#update-shop-dialog #spLevelDiamondPrice").val();//钻石会员价格
            var spLevelGoldPrice = $("#update-shop-dialog #spLevelGoldPrice").val();//金牌会员价格
            var spLevelSilverPrice = $("#update-shop-dialog #spLevelSilverPrice").val();//银牌会员价格
            var spLevelCopperPrice = $("#update-shop-dialog #spLevelCopperPrice").val();//铜牌会员
            //商品图片
            var sDesc = Shop.editor.html();//商品描述
            
            if(Util.isEmpty(sName)){
                $("#sNameSpan").html("请输入商品名称。");
                $("#update-shop-dialog #sName").focus();
                return;
            }
            $("#sNameSpan").html("");
            if(Util.isEmpty(sMobileName)){
                $("#sMobileNameSpan").html("请输入商品名称。");
                $("#update-shop-dialog #sMobileName").focus();
                return;
            }
            $("#sMobileNameSpan").html("");
            if(Util.isEmpty(sNo)){
                $("#sNoSpan").html("请输入商品编号。");
                $("#update-shop-dialog #sNo").focus();
                return;
            }
            $("#sNoSpan").html("");
            if(Util.isEmpty(sShopInventoryNum)){
                $("#sShopInventoryNumSpan").html("请输入商品库存数量。");
                $("#update-shop-dialog #sShopInventoryNum").focus();
                return;
            }
            $("#sShopInventoryNumSpan").html("");
            
            if(Util.isEmpty(spUnit)){
                $("#spUnitSpan").html("请输入商品单位。");
                $("#update-shop-dialog #spUnit").focus();
                return;
            }
            $("#spUnitSpan").html("");
            if(Util.isEmpty(spSpecification)){
                $("#spSpecificationSpan").html("请输入商品单位。");
                $("#update-shop-dialog #spSpecification").focus();
                return;
            }
            $("#spSpecificationSpan").html("");
            if(Util.isEmpty(spUnit)){
                $("#spUnitSpan").html("请输入商品规格。");
                $("#update-shop-dialog #spUnit").focus();
                return;
            }
            $("#spSpecificationSpan").html("");
            if(Util.isEmpty(spMarketPrice)){
                $("#spMarketPriceSpan").html("请输入商品。");
                $("#update-shop-dialog #spMarketPrice").focus();
                return;
            }
            $("#spMarketPriceSpan").html("");
            if(Util.isEmpty(spPrice)){
                $("#spPriceSpan").html("请输入商品市场价。");
                $("#update-shop-dialog #spPrice").focus();
                return;
            }
            $("#spPriceSpan").html("");
            if(Util.isEmpty(spUnitNum)){
                $("#spUnitNumSpan").html("请输入商品数量转换。");
                $("#update-shop-dialog #spUnitNum").focus();
                return;
            }
            $("#spUnitNumSpan").html("");
            if(Util.isEmpty(spLevelDiamondPrice)){
                $("#spLevelDiamondPriceSpan").html("请输入钻石会员价格。");
                $("#update-shop-dialog #spLevelDiamondPrice").focus();
                return;
            }
            $("#spLevelDiamondPriceSpan").html("");
            if(Util.isEmpty(spLevelGoldPrice)){
                $("#spLevelGoldPriceSpan").html("请输入金牌会员价格。");
                $("#update-shop-dialog #spLevelGoldPrice").focus();
                return;
            }
            $("#spLevelGoldPriceSpan").html("");
            if(Util.isEmpty(spLevelSilverPrice)){
                $("#spLevelSilverPriceSpan").html("请输入银牌会员价格。");
                $("#update-shop-dialog #spLevelSilverPrice").focus();
                return;
            }
            $("#spLevelSilverPriceSpan").html("");
            if(Util.isEmpty(spLevelCopperPrice)){
                $("#spLevelCopperPriceSpan").html("请输入铜牌会员价格。");
                $("#update-shop-dialog #spLevelCopperPricePrice").focus();
                return;
            }
            $("#spLevelCopperPriceSpan").html("");
            
            if(Util.isEmpty(sDesc)){
                $("#sDescSpan").html("请输入商品描述信息。");
                $("#add-shop-dialog #sDesc").focus();
                return;
            }
            $("#sDescSpan").html("");
            
            $.ajaxFileUpload({
                url:t.path+"/busShop/updateShop.do",
                secureuri:false,
                fileElementId:["sImgSmall","sImgBig1","sImgBig2","sImgBig3","sImgBig4","sImgBig5"],
                dataType:"json",
                cache:false,
                data:{
                    sId:sId,
                    sName:sName,
                    sMobileName:sMobileName,
                    sNo:sNo,
                    sShopTypeId:sShopTypeId,
                    sShopKeyWord:sShopKeyWord,
                    shopLabels:shopLabels,
                    sShopInventoryNum:sShopInventoryNum,
                    aShopSellNum:aShopSellNum,
                    sIsPutaway:sIsPutaway,
                    sSort:sSort,
                    spUnit:spUnit,
                    spSpecification:spSpecification,
                    spMarketPrice:spMarketPrice,
                    spCostPrice:spCostPrice,
                    spPrice:spPrice,
                    spUnitNum:spUnitNum,
                    spLevelDiamondPrice:spLevelDiamondPrice,
                    spLevelGoldPrice:spLevelGoldPrice,
                    spLevelSilverPrice:spLevelSilverPrice,
                    spLevelCopperPrice:spLevelCopperPrice,
                    sDesc:sDesc
                },
                success:function(data,status){//服务器响应成功处理
                    if(data.success){
                        //单独保存商品详情
                        $.ajax({
                            type:"POST",
                            url:t.path+"/busShop/saveShopDesc.do",
                            dataType:"json",
                            data:{
                                sId:sId,
                                sDesc:sDesc
                            },
                            async: false,
                            cache:false,
                            success:function(data,status){
                                if(data.success){
                                    alert("保存成功!");
                                    t.initTablePage();
                                }else{
                                    alert("保存失败!");
                                }
                            }
                        });
                    }else{
                        alert("保存失败!");
                    }
                },
                error:function(data,status,e){//服务器响应失败处理
                    alert(e);   
                }
            });
            
        },
        //删除节点
        deleteShopDialog:function(id){
            bootbox.confirm({
                size : 'small',
                message : "确认删除该商品，删除后将不可恢复？",
                callback : function(result) {
                    if (result) {
                        $.ajax({
                            type:"POST",
                            url:t.path+"/busShop/deleteShop.do",
                            dataType:"json",
                            data:{
                                id:id
                            },
                            async: false,
                            cache:false,
                            success:function(data){
                                if (data && data.success) {
                                    alert("删除成功。");
                                    t.initTablePage();
                                } else {
                                    alert("删除失败。");
                                }
                            }
                        });
                    }
                }
            });
        },
        //验证商品编号
        checkShopNo:function(shopNo){
            $.ajax({
                type:"POST",
                url:t.path+"/busShop/checkShopNo.do",
                dataType:"json",
                data:{
                    shopNo:shopNo
                },
                async: false,
                cache:false,
                success:function(data){
                    if (data) {
                        if(!data.success){
                            alert(data.msg);
                            $("#sNo").focus();
                            $("#sNoSpan").html(data.msg);
                        }
                    }
                }
            });
        },
        //上下架
        updatePutaway:function(id){
            bootbox.confirm({
                size : 'small',
                message : "确认执行该操作？",
                callback : function(result) {
                    if (result) {
                        $.ajax({
                            type:"POST",
                            url:t.path+"/busShop/updatePutaway.do",
                            dataType:"json",
                            data:{
                                id:id
                            },
                            async: false,
                            cache:false,
                            success:function(data){
                                if (data) {
                                    if(data.success){
                                        alert("操作成功。");
                                        $("#shop-table").bootstrapTable('refresh');
                                    }else{
                                        alert("操作失败。");
                                    }
                                }
                            }
                        });
                    }
                }
            });
            
        },
        putawayFormatter:function(value,row){
            if(value==1){
                return '上架';
            }else{
                return "<font style='color:red;'>下架</fong>";
            }
        },
        imgSmallFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return "<img src='"+t.path+"/upload/"+value+"' style='width:70px;height:70px;'/>";
            }
        },
        dateFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return value.substring(0,10);
            }
        },
        urlFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return "<a href='"+value+"' target='_blank'>"+value+"</a>";
            }
        },
        interfaceNumFormatter:function(value,row){
            return ' <a href="javascript:ShopInterface.loadShopInterfaceDialog(\''+row.hjlIsId+'\')">'+value+' </a>';
        },
        //商品单位
        priceUnitFormatter:function(value,row){
            if(value!=null){
                return value.spUnit;
            }
        },
        //市场价格
        priceMarketPriceFormatter:function(value,row){
            if(value!=null){
                return value.spMarketPrice;
            }
        },
        //成本价
        priceCostPriceFormatter:function(value,row){
            if(value!=null){
                return value.spCostPrice;
            }
        },
        //商品类别
        typeFormatter:function(value,row){
            if(value!=null){
                return value.stName;
            }
        },
        operator : function(value, row) {
            var html = "";
            html += '<a href="javascript:Shop.updatePutaway(\'' + value + '\')" class="btn btn-default btn-xs">上|下架</a>&nbsp;';
            html += '<a href="javascript:ShopPrice.updateShopPriceDialog(\'' + value + '\')" class="btn btn-default btn-xs">改价</a>&nbsp;';
            html += '<a href="javascript:Shop.updateShopDialog(\'' + value + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp;';
            html += '<a href="javascript:Shop.deleteShopDialog(\'' + value + '\')" class="btn btn-default btn-xs">删除</a>';
            return html;
        }
    }
    return t;
}();